'use strict';

/**
 * @param {Egg.Application} app - egg application
 */

function bind(router, controller, path = '/') {

  for (let p of Object.keys(controller)) {
    if (typeof (controller[p]) == 'function') {
      router.all(path + p, controller[p]);
      console.log('bind', path + p);
    } else {

      bind(router, controller[p], path + p + '/');
    }
  }
  return true;
}

function bind2(router, controller) {
  for (let m of Object.keys(controller)) {
    //绑定index
    if (typeof (controller[m]['index']['index']) != 'undefined') {
      router.all(`/${m}`, controller[m]['index']['index']);
    }
    for (let c of Object.keys(controller[m])) {
      //绑定index
      if (typeof (controller[m][c]['index']) != 'undefined') {
        router.all(`/${m}/${c}`, controller[m][c]['index']);
      }
      for (let a of Object.keys(controller[m][c])) {
        router.all(`/${m}/${c}/${a}`, controller[m][c][a]);
        // console.log('bind', `/${m}/${c}/${a}`);
      }

    }

  }
  return true;
}
module.exports = app => {
  const { router, controller } = app;
  // console.log(controller);
  // bind(router, controller);
  bind2(router, controller);

  router.get('/', controller.index.index.index);
  // router.get('/test', controller.home.test);
};
